package com.ruoyi.base.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.base.mapper.BaseBuildingMapper;
import com.ruoyi.base.mapper.BaseRoomMapper;
import com.ruoyi.base.domain.BaseBuilding;
import com.ruoyi.base.domain.BaseRoom;
import com.ruoyi.base.service.IBaseRoomService;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;

/**
 * 房间Service业务层处理
 * 
 * @author yepanpan
 * @date 2020-11-09
 */
@Service
public class BaseRoomServiceImpl implements IBaseRoomService 
{
    private static final Logger log = LoggerFactory.getLogger(BaseRoomServiceImpl.class);
    @Autowired
    private BaseRoomMapper baseRoomMapper;
    @Autowired
    private BaseBuildingMapper baseBuildingMapper;

    /**
     * 查询房间
     * 
     * @param id 房间ID
     * @return 房间
     */
    @Override
    public BaseRoom selectBaseRoomById(Long id)
    {
        return baseRoomMapper.selectBaseRoomById(id);
    }

    /**
     * 查询房间列表
     * 
     * @param baseRoom 房间
     * @return 房间
     */
    @Override
    public List<BaseRoom> selectBaseRoomList(BaseRoom baseRoom)
    {
        return baseRoomMapper.selectBaseRoomList(baseRoom);
    }
    
    /**
     * 查询房间下拉列表
     * 
     * @param baseRoom 房间
     * @return 房间
     */
    @Override
    public List<BaseRoom> selectList(BaseRoom baseRoom)
    {
        return baseRoomMapper.selectList(baseRoom);
    }
    
    /**
     * 查询可用固定教室列表
     * 
     * @param baseRoom 房间
     * @return 房间集合
     */
    @Override
    public List<BaseRoom> selectClassRoom(BaseRoom baseRoom){
    	return baseRoomMapper.selectClassRoom(baseRoom);
    }

    /**
     * 新增房间
     * 
     * @param baseRoom 房间
     * @return 结果
     */
    @Override
    public int insertBaseRoom(BaseRoom baseRoom)
    {
        return baseRoomMapper.insertBaseRoom(baseRoom);
    }

    /**
     * 修改房间
     * 
     * @param baseRoom 房间
     * @return 结果
     */
    @Override
    public int updateBaseRoom(BaseRoom baseRoom)
    {
        return baseRoomMapper.updateBaseRoom(baseRoom);
    }

    /**
     * 批量删除房间
     * 
     * @param ids 需要删除的房间ID
     * @return 结果
     */
    @Override
    public int deleteBaseRoomByIds(Long[] ids)
    {
        return baseRoomMapper.deleteBaseRoomByIds(ids);
    }

    /**
     * 删除房间信息
     * 
     * @param id 房间ID
     * @return 结果
     */
    @Override
    public int deleteBaseRoomById(Long id)
    {
        return baseRoomMapper.deleteBaseRoomById(id);
    }

    /**
     * 导入房间数据
     * 
     * @param roomList 房间数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
	@Override
    @Transactional
	public String importRoom(List<BaseRoom> roomList, Boolean isUpdateSupport, String operName) {
		if (StringUtils.isNull(roomList) || roomList.size() == 0)
        {
            throw new CustomException("导入房间数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BaseRoom vo : roomList)
        {
            try
            {
                // 验证楼宇是否存在
            	BaseBuilding bc = new BaseBuilding();
            	bc.setTitle(vo.getBuildName());
            	BaseBuilding build = baseBuildingMapper.findBaseBuilding(bc);
            	if(build == null) {
            		throw new CustomException("楼宇不"+vo.getBuildName()+"存在");
            	}
            	vo.setBuildId(build.getId());
            	BaseRoom cond = new BaseRoom();
            	cond.setBuildId(build.getId());
            	cond.setTitle(vo.getTitle());
            	BaseRoom old = baseRoomMapper.findBaseRoom(cond);
                if (StringUtils.isNull(old))
                {
                    this.insertBaseRoom(vo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、房间 " + vo.getTitle() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    vo.setId(old.getId());
                    this.updateBaseRoom(vo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、房间 " + vo.getTitle() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、房间 " + vo.getTitle() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、房间 " + vo.getTitle() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
	}
}
